import { useContext } from "react";
import usePlatform from "./usePlatform";
import { APP_ROUTE, PATH_ROUTE, PLATFORM_TYPE } from "./utils";
import { useModel } from "@supcon/supcon-microapp";
import { useAppData } from "@/renderer";

function useJumpFun() {
  const { history } = useAppData();
  const currentPlatform = usePlatform();
  const { openTab } = useModel("@@qiankunStateFromMaster") || {};
  // const { prefix } = useContext(routeContext);
  const prefix = "";
  const jumpFunc = (url: string, isSelf = false, openWindow = true) => {
    const hasParam = url.includes("?");
    const newUrl = openWindow ? `${url}${hasParam ? "&newTab=true" : ""}` : url;

    if (isSelf) {
      // 自身打开页面的逻辑（大屏，自身页面）
      openWindow
        ? window.open(
            window.location.protocol +
              "//" +
              window.location.host +
              prefix +
              newUrl
          )
        : (window.location.href =
            window.location.protocol +
            "//" +
            window.location.host +
            prefix +
            newUrl);
    } else {
      // 普通页面的逻辑
      if (currentPlatform === PLATFORM_TYPE.dev) {
        openWindow
          ? window.open(prefix + newUrl)
          : history.push(prefix + newUrl);
      } else if (currentPlatform === PLATFORM_TYPE.obp) {
        // openTab(APP_ROUTE + newUrl);
      } else if (currentPlatform === PLATFORM_TYPE.supOS) {
        console.log("进入 supOS");

        openWindow
          ? window.open(`${PATH_ROUTE}${prefix}` + newUrl)
          : history.push(prefix + newUrl);
      } else {
        openWindow
          ? window.open(`${window.location.host}${prefix}` + newUrl)
          : history.push(prefix + newUrl);
      }
    }
  };

  return jumpFunc;
}

export default useJumpFun;
